package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseCategory1;
import com.atguigu.gmall.product.entity.BaseCategory2;
import com.atguigu.gmall.product.entity.BaseCategory3;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.BaseCategory2Service;
import com.atguigu.gmall.product.service.BaseCategory3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lfy
 * @Description 分类请求处理
 * @create 2023-07-11 11:41
 */
@RestController
public class CategoryRestController {


    @Autowired
    BaseCategory1Service baseCategory1Service;

    @Autowired
    BaseCategory2Service baseCategory2Service;

    @Autowired
    BaseCategory3Service baseCategory3Service;

    /**
     * 获取所有一级分类
     * @return
     */
    @GetMapping("/admin/product/getCategory1")
    public Result getAllCategory1() {
        //业务：获取所有一级分类
        List<BaseCategory1> list = baseCategory1Service.list();

        return Result.ok(list);
    }


    /**
     * 根据一级分类id找二级分类
     * @return
     */
    @GetMapping("/admin/product/getCategory2/{c1id}")
    public Result getCategory2ByC1Id(@PathVariable("c1id") Long c1id){


        //根据一级分类id找二级分类
        List<BaseCategory2> category2s = baseCategory2Service.lambdaQuery()
                .eq(BaseCategory2::getCategory1Id, c1id)
                .list();

        return Result.ok(category2s);
    }

    /**
     * 根据二级分类id找到它的所有三级分类
     * @return
     */
    @GetMapping("/admin/product/getCategory3/{c2id}")
    public Result getCategory3ByC2Id(@PathVariable("c2id") Long c2id){


        List<BaseCategory3> list = baseCategory3Service.lambdaQuery()
                .eq(BaseCategory3::getCategory2Id, c2id)
                .list();

        return Result.ok(list);
    }

}
